let ws2 = null

function online() {
    let nickname = localStorage.getItem("nickname");
    /*写一个礼物飘过的效果*/
    ws2 = new WebSocket("ws://localhost:8080/gift/"+nickname);
    //ws四种状态
    ws2.onopen = function(){
        console.log("连接成功");
    }
    ws2.onclose = function(){
        console.log("连接关闭");
        setTimeout(()=>{//心跳，用于自动重连
            online()
        },2000);
    }
    ws2.onmessage = function(e){
        const data = JSON.parse(e.data);
        render(data)
        console.log(e.data);
    }
    ws2.onerror = function(e){
        console.log(e);
    }
}

online()